//
//  Version.m
//  TengyunStation
//
//  Created by Johnny on 15/9/6.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "Version.h"
#import "SocketMethod.h"


@implementation Version

- (id)initWithData:(NSData *)data {
    self = [super init];
    if (self) {
        int length = 0;
        self.versionCode = [SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)];
        length += 16;
        self.versionSize = [SocketMethod integerSubdata:data Range:NSMakeRange(length, 4)];
        length += 4;
        self.versionMD5 = [SocketMethod stringSubdata:data Range:NSMakeRange(length, 32)];
        length += 32;
        NSInteger updateLogLength = [SocketMethod integerSubdata:data Range:NSMakeRange(length, 4)];
        length += 4;
        self.updateLog = [SocketMethod stringSubdata:data Range:NSMakeRange(length, updateLogLength)];
        length += updateLogLength;
        self.updateTime = [SocketMethod stringSubdata:data Range:NSMakeRange(length, 16)];
        length += 16;
        self.isForce = [[SocketMethod stringSubdata:data Range:NSMakeRange(length, 1)] boolValue];
        length += 1;
        self.downloadUrl = [SocketMethod stringSubdata:data Range:NSMakeRange(length, 128)];
    }
    return self;
}

- (void)setVersionCode:(NSString *)versionCode {
    _versionCode = versionCode;
    if (versionCode.length >= 8) {
        self.version = [NSString stringWithFormat:@"V%@",[versionCode substringWithRange:NSMakeRange(3, 5)]];
    }
    NSString *currentVersion = [SocketMethod getSocketVersion];
    if ([currentVersion compare:versionCode] == NSOrderedAscending) {
        self.isHadNewVersion = YES;
    } else {
        self.isHadNewVersion = NO;
    }
}

@end
